package com.wc.alorithm_blue_bridge._思维.连连看;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/12 16:54
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3444436&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * |a - c| = |b - d| > 0, 也就是表示
     * 找到 y = x + b以及 y = -x + b 的全部相等的值即可
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    static int[][] g = new int[N][N];
    static int[] num = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) g[i][j] = sc.nextInt();
        }
        int res = 0;
        // y = x + b
        for (int i = 2 - n; i <= m; i++) {
            Arrays.fill(num, 0);
            int b = i - 1;
            for (int y = i; y <= n + m - 1; y++) {
                int x = y - b;
                if (x < 1 || x > n || y < 1 || y > m) continue;
                num[g[x][y]]++;
            }
            for (int j = 1; j < N; j++) res += num[j] * (num[j] - 1);
        }

        // y = -x + b
        for (int i = 1; i <= n + m - 1; i++) {
            Arrays.fill(num, 0);
            int b = i + 1;
            for (int y = i; y >= -n + 2; y--) {
                int x = b - y;
                if (x < 1 || x > n || y < 1 || y > m) continue;
                num[g[x][y]]++;
            }
            for (int j = 1; j < N; j++) res += num[j] * (num[j] - 1);
        }

        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

